package net.cn_su.sharpeyes.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ProgressBar;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import net.cn_su.sharpeyes.R;
import net.cn_su.sharpeyes.pref.ServerInfo_;
import net.cn_su.sharpeyes.utils.UIUtil;
import net.cn_su.sharpeyes.utils.http.HttpClient;
import net.cn_su.sharpeyes.utils.http.HttpUrl;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Fullscreen;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

/**
 * Created by yzy on 2016/12/2.
 */
@EActivity(R.layout.activity_update_pwd)
@Fullscreen
public class UpdatePwdActivity extends AppCompatActivity {

    @ViewById
    EditText et_old_pwd, et_new_pwd, et_pwd_r;

    @ViewById
    ProgressBar progressBar;


    @Pref
    ServerInfo_ mServerInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }


    @Click(R.id.btn_back)
    protected void btnBack() {
        finish();
    }

    @Click(R.id.tv_confirm)
    protected void btnSaveClick() {
        String oldPwd = et_old_pwd.getText().toString();
        final String newPwd = et_new_pwd.getText().toString();
        String newRePwd = et_pwd_r.getText().toString();

        if (TextUtils.isEmpty(oldPwd)) {
            UIUtil.showToast(this, "请输入原密码");
            et_old_pwd.requestFocus();
            return;
        }

        if (newPwd.equals(oldPwd)) {
            UIUtil.showToast(this, "新密码和原密码相同");
            et_new_pwd.requestFocus();
            return;
        }
        if (newPwd.length() < 8) {
            UIUtil.showToast(this, "密码至少8位数");
            et_old_pwd.requestFocus();
            return;
        }
        if (newRePwd.length() < 8) {
            UIUtil.showToast(this, "确认密码至少8位数");
            et_new_pwd.requestFocus();
            return;
        }
        if (!newPwd.equals(newRePwd)) {
            UIUtil.showToast(this, "两次输入的密码不一致");
            et_pwd_r.requestFocus();
            return;
        }

        progressBar.setVisibility(View.VISIBLE);

        final RequestParams params = new RequestParams();
        params.add("user_id", mServerInfo.userId().get() + "");
        params.add("old_password", newPwd);
        params.add("new_password", newRePwd);

        HttpClient.post(this, HttpUrl.APP_RESET_PSW, params, new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);

                if (progressBar != null) {
                    progressBar.setVisibility(View.GONE);
                }
                try {
                    int code = response.getInt("code");
                    String msg = response.getString("msg");
                    UIUtil.showToast(UpdatePwdActivity.this, msg);
                    if (code != -1) {
                        mServerInfo.localPassword().put(newPwd);
                        finish();
                    } else {
                        et_old_pwd.requestFocus();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });

    }

}
